package com.lzy.shops.controller.backstage;

import com.alibaba.fastjson.JSON;
import com.fasterxml.jackson.annotation.JsonFormat;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.lzy.shops.bean.Order;
import com.lzy.shops.bean.PageBean;
import com.lzy.shops.bean.Search;
import com.lzy.shops.service.OrderService;
import com.lzy.shops.utils.PageBeanUtils;
import com.lzy.shops.utils.ResponseData;
import com.lzy.shops.vo.OrderVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/system/order")
public class OrderController {

    @Autowired
    private OrderService service;

    ObjectMapper objectMapper = new ObjectMapper();

    @GetMapping("/findByUserOrders")
    public String findByUserOrders(@RequestParam Map<String,Object> maps) throws JsonProcessingException {
        ResponseData<Object> responseData = null;
        PageBean<OrderVo> pageBean = new PageBean<>();
        try {
            String userId = (String) maps.get("userId");
            String pageNo = (String) maps.get("pageNo");
            String pageSize = (String) maps.get("pageSize");
            Integer end = Integer.parseInt(pageSize);
            Integer start = ((Integer.parseInt(pageNo)<=0?1:Integer.parseInt(pageNo))-1)*end;
            List<OrderVo> list = service.findByUserOrder(Integer.parseInt(userId));
            Integer count = service.findByUserOrderNums(Integer.parseInt(userId));
            pageBean = PageBeanUtils.run(list, start, end);
            pageBean.setPageNo(Integer.parseInt(pageNo));
            pageBean.setTotalRecords(count);
            responseData = new ResponseData<Object>(true,pageBean);
        } catch (NumberFormatException e) {
            responseData = new ResponseData<Object>(false,"数据异常");
        }
        return objectMapper.writeValueAsString(responseData);
    }

    @GetMapping("/findAllOrder")
    public String findAllOrder(@RequestParam Map<String,Object> maps) throws JsonProcessingException {
        ResponseData<Object> responseData = null;
        PageBean<OrderVo> pageBean = null;
        HashMap<String,String> hashMap = null;
        try {
            String pageNo = (String) maps.get("pageNo");
            String pageSize = (String) maps.get("pageSize");
            String search = (String) maps.get("search");
            Integer end = Integer.parseInt(pageSize);
            Integer start = (Integer.parseInt(pageNo)-1)*end;
            if(search!=null){
                hashMap = JSON.parseObject(search, HashMap.class);
            }
            Search searchBean = new Search(start,end,hashMap);
            List<OrderVo> allOrder = service.findAllOrder(searchBean);
            Integer allOrderNums = service.findAllOrderNums();
            pageBean = new PageBean<OrderVo>();
            pageBean.setList(allOrder);
            pageBean.setPageSize(end);
            pageBean.setPageNo(Integer.parseInt(pageNo));
            pageBean.setTotalRecords(allOrderNums);
            responseData = new ResponseData<Object>(true,pageBean);
        } catch (NumberFormatException e) {
            responseData = new ResponseData<Object>(false,e.getMessage());
        }

        return objectMapper.writeValueAsString(responseData);
    }
}
